/**
 * 
 */
package evance.server.common.dao;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import com.mysema.query.BooleanBuilder;
import com.mysema.query.jpa.JPQLQuery;
import com.mysema.query.jpa.impl.JPAQuery;
import com.trg.dao.jpa.GenericDAOImpl;

import evance.data.model.entity.EvLoginInfo;
import evance.data.model.entity.QEvLoginInfo;
import evance.server.common.constants.UnitName;

/**
 * @author BaoVuong
 *
 */
@Repository
public class LoginInfoDAOImpl extends GenericDAOImpl<EvLoginInfo, Long> implements LoginInfoDAO {

	@PersistenceContext(unitName = UnitName.EVANCE)
	@Override
	public void setEntityManager(EntityManager entityManager) {
		super.setEntityManager(entityManager);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see evance.server.common.dao.LoginInfoDAO#getLoginInfo(java.lang.String)
	 */
	@Override
	public EvLoginInfo getLoginInfo(String username) {
		JPQLQuery query = new JPAQuery(em());
		QEvLoginInfo qEvLoginInfo = QEvLoginInfo.evLoginInfo;
		BooleanBuilder builder = new BooleanBuilder();
		builder.and(qEvLoginInfo.username.eq(username));
		
		EvLoginInfo result = query.from(qEvLoginInfo).where(builder).uniqueResult(qEvLoginInfo);
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * evance.server.common.dao.LoginInfoDAO#checkExistUsername(java.lang.String
	 * )
	 */
	@Override
	public boolean checkExistUsername(String username) {
		JPQLQuery query = new JPAQuery(em());
		QEvLoginInfo qEvLoginInfo = QEvLoginInfo.evLoginInfo;
		BooleanBuilder builder = new BooleanBuilder();
		builder.and(qEvLoginInfo.username.eq(username));
		EvLoginInfo result = query.from(qEvLoginInfo).where(builder).uniqueResult(qEvLoginInfo);
		if (result != null) {
			return true;
		}
		return false;
	}

}
